package com.brtbeacon.demo.lot.speech;

import android.content.Context;
import android.content.res.AssetManager;
import android.os.Handler;

import com.baidu.tts.auth.AuthInfo;
import com.baidu.tts.client.SpeechError;
import com.baidu.tts.client.SpeechSynthesizer;
import com.baidu.tts.client.SpeechSynthesizerListener;
import com.baidu.tts.client.TtsMode;
import com.brtbeacon.demo.lot.utils.FileUtil;

import java.io.IOException;

public class SpeechEngineBaiduTTS extends SpeechEngine {

    protected SpeechSynthesizer mSpeechSynthesizer;
    private Context context;
    private Handler handler = new Handler();

    protected String appId = "19572294";
    protected String appKey = "a5zc4IAowKnVmFv1bIgqiGGN";
    protected String secretKey = "HtOY6Pum87Gz210NzZbo87uwqVpzKPgU";
    protected TtsMode ttsMode = TtsMode.MIX;
    protected String ttsTextDataFileName;
    protected String ttsTextDataPath;
    protected String ttsModelDataFileName;
    protected String ttsModelDataPath;

    public SpeechEngineBaiduTTS(Context context) {
        this.context = context;
        this.ttsTextDataFileName = "bd_etts_text.dat";
        this.ttsTextDataPath = context.getExternalFilesDir("") + this.ttsTextDataFileName;
        this.ttsModelDataFileName = "bd_etts_common_speech_f7_mand_eng_high_am-mix_v3.0.0_20170512.dat";
        this.ttsModelDataPath = context.getExternalFilesDir("") + this.ttsModelDataFileName;
    }

    @Override
    public boolean init() {
        mSpeechSynthesizer = SpeechSynthesizer.getInstance();
        mSpeechSynthesizer.setContext(context);
        mSpeechSynthesizer.setSpeechSynthesizerListener(speechSynthesizerListener);
        mSpeechSynthesizer.setAppId(appId);
        mSpeechSynthesizer.setApiKey(appKey, secretKey);

        AuthInfo authInfo = mSpeechSynthesizer.auth(ttsMode);
        if (!authInfo.isSuccess()) {
            return false;
        }

        try {
            initSpeechData();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_SPEAKER, "0");
        // 设置合成的音量，0-9 ，默认 5
        mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_VOLUME, "5");
        // 设置合成的语速，0-9 ，默认 5
        mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_SPEED, "5");
        // 设置合成的语调，0-9 ，默认 5
        mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_PITCH, "5");
        // MIX_MODE_HIGH_SPEED_SYNTHESIZE_WIFI wifi状态下使用在线，非wifi离线。在线状态下， 请求超时1.2s自动转离线
        mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_MIX_MODE, SpeechSynthesizer.MIX_MODE_HIGH_SPEED_SYNTHESIZE_WIFI);
        // 设置本地文本数据
        mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_TTS_TEXT_MODEL_FILE, ttsTextDataPath);
        // 设置本地语音数据
        mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_TTS_SPEECH_MODEL_FILE, ttsModelDataPath);

        int result = mSpeechSynthesizer.initTts(ttsMode);
        if (result != 0) {
            return false;
        }

        return true;
    }


    @Override
    public void speak(String content) {
        mSpeechSynthesizer.speak(content);
    }

    @Override
    public void stop() {
        mSpeechSynthesizer.stop();
    }

    @Override
    public void close() {
        super.close();
        if (mSpeechSynthesizer != null) {
            mSpeechSynthesizer.stop();
            mSpeechSynthesizer.release();
            mSpeechSynthesizer = null;
        }
    }

    @Override
    public String getName() {
        return "BaiduTTS";
    }

    private void initSpeechData() throws IOException {
        copyAssetsFile(ttsTextDataFileName, ttsTextDataPath);
        copyAssetsFile(ttsModelDataFileName, ttsModelDataPath);
    }

    private void copyAssetsFile(String sourceFilename, String destPath) throws IOException {
        AssetManager assets = context.getApplicationContext().getAssets();
        FileUtil.copyFromAssets(assets, sourceFilename, destPath, true);
    }

    private SpeechSynthesizerListener speechSynthesizerListener = new SpeechSynthesizerListener() {
        @Override
        public void onSynthesizeStart(String s) {

        }

        @Override
        public void onSynthesizeDataArrived(String s, byte[] bytes, int i) {

        }

        @Override
        public void onSynthesizeFinish(String s) {

        }

        @Override
        public void onSpeechStart(String s) {

        }

        @Override
        public void onSpeechProgressChanged(String s, int i) {

        }

        @Override
        public void onSpeechFinish(String s) {

        }

        @Override
        public void onError(String s, SpeechError speechError) {

        }
    };
}
